/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import dao.phieuxuatDAO;
import gui.PhieuXuatHangInternalFrame;
import dao.hanghoaDAO;
import dao.chitietxuathangDAO;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import pojo.Hanghoa;
import pojo.Phieuxuathang;
import pojo.Chitietphieuxuathang;

/**
 *
 * @author HoaDinhGiang
 */
public class ChiTietPhieuXuatInternalFrame extends javax.swing.JInternalFrame {

    private DefaultTableModel dtm;
    private List<Hanghoa> dsHangHoa;
    private Integer indexDuocChon;
    protected  Phieuxuathang _phieuXuatHang;
    protected double _tongTienPhieuXuat;
    /**
     * Creates new form ChiTietPhieuXuatInternalFrame
     */
    public ChiTietPhieuXuatInternalFrame() {
        initComponents();
        dtm = new DefaultTableModel();
        dtm.addColumn("Mã");
        dtm.addColumn("Tên Sản Phẩm");
        dtm.addColumn("Loại Sản Phẩm");
        dtm.addColumn("Đơn Vị");
        dtm.addColumn("Giá Bán");
        dtm.addColumn("Giá Tối Thiểu");
        dtm.addColumn("Giá Tối Đa");
        dtm.addColumn("Nhà Phân Phối");
        dtm.addColumn("Nhà Sản Xuất");
        dtm.addColumn("Kho");
        dtm.addColumn("Hàng Tồn");
        this._tongTienPhieuXuat = 0;
    }

    public void hienThiHangHoa() {

        jTable1.removeAll();
        dsHangHoa = hanghoaDAO.layDanhSachHangHoaTonTai();
        //Tạo model cho Table


        dtm.setNumRows(dsHangHoa.size());
        for (int i = 0; i < dsHangHoa.size(); i++) {
            Hanghoa hh = dsHangHoa.get(i);
            dtm.setValueAt(hh.getMahanghoa(), i, 0);
            dtm.setValueAt(hh.getTenhanghoa(), i, 1);
            dtm.setValueAt(hh.getLoaihanghoa().getTenloaihanghoa(), i, 2);
            dtm.setValueAt(hh.getDonvitinh().getTendonvitinh(), i, 3);
            dtm.setValueAt(hh.getGiaban(), i, 4);
            dtm.setValueAt(hh.getGiatoithieu(), i, 5);
            dtm.setValueAt(hh.getGiatoida(), i, 6);
            dtm.setValueAt(hh.getNhaphanphoi().getTennhaphanphoi(), i, 7);
            dtm.setValueAt(hh.getNhasanxuat().getTennhasanxuat(), i, 8);
            dtm.setValueAt(hh.getKhohang().getTenkhohang(), i, 9);
            dtm.setValueAt(hh.getHangton(), i, 10);
        }
        //Hiển thị lên Table
        this.jTable1.setModel(dtm);
        this.jTable1.changeSelection(0, 0, false, false);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jbtXuatHang = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();
        jtfSoLuong = new javax.swing.JTextField();
        jtfMaPhieuXuat = new javax.swing.JTextField();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Lập Chi Tiết Phiếu Xuất");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        jLabel1.setText("Mã Phiếu Xuất:");

        jLabel2.setText("Số Lượng:");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Sản phẩm"));

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)
        );

        jbtXuatHang.setText("Xuất Hàng");
        jbtXuatHang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXuatHangActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        jtfMaPhieuXuat.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtThoat))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfSoLuong)
                            .addComponent(jtfMaPhieuXuat, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                        .addGap(71, 71, 71)
                        .addComponent(jbtXuatHang, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 426, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtfMaPhieuXuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jbtXuatHang)
                    .addComponent(jtfSoLuong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtThoat)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        //cap nhat lai tongtien trong phieuxuathang
        _phieuXuatHang.setTongtien(_tongTienPhieuXuat);
        phieuxuatDAO.capNhatThongTinPhieuXuat(_phieuXuatHang);
        this.dispose();
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        int str = _phieuXuatHang.getMaphieuxuathang();
        jtfMaPhieuXuat.setText(String.valueOf(str)) ;
        hienThiHangHoa();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        //lay Object hanghoa duoc chon trong table
        indexDuocChon = jTable1.getSelectedRow();


    }//GEN-LAST:event_jTable1MouseClicked

    private void jbtXuatHangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXuatHangActionPerformed
        // TODO add your handling code here:
        //1-kiem tra so luong ton du cho xuat hay khong.
        int soLuongXuat = Integer.parseInt(jtfSoLuong.getText());
        int soLuongTon = dsHangHoa.get(indexDuocChon).getHangton();
        if (soLuongXuat > soLuongTon) {
            JOptionPane.showMessageDialog(this, "Hàng không đủ trong kho.", "Thông Báo", JOptionPane.WARNING_MESSAGE);
            return;
        }
        int hangTonMoi = soLuongTon - soLuongXuat;
        
        //2-Cap nhat lai so luong ton.
        Hanghoa newHanghoa = dsHangHoa.get(indexDuocChon);
        newHanghoa.setHangton(hangTonMoi);
        hanghoaDAO.capNhatThongTinHangHoa(newHanghoa);
        
        //3-Them Chi tiet xuat hang moi
        Chitietphieuxuathang newCTXH = new Chitietphieuxuathang();
        newCTXH.setPhieuxuathang(_phieuXuatHang);
        newCTXH.setHanghoa(newHanghoa);
        newCTXH.setSoluong(soLuongXuat);
        double thanhTien = soLuongXuat*newHanghoa.getGiaban();
        newCTXH.setThanhtien(thanhTien);
         
        this._tongTienPhieuXuat+=thanhTien;
        boolean kq = chitietxuathangDAO.themChiTietHangHoa(newCTXH);
        if (kq == true) {
            this._tongTienPhieuXuat+=thanhTien;
      //      hienThiPhieuXuatTonTai();
            
        } else {
            JOptionPane.showMessageDialog(this, "Thêm chi tiết xuất hàng thất bại");
        }
      //  newCTXH.s
        hienThiHangHoa();
       
    }//GEN-LAST:event_jbtXuatHangActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtXuatHang;
    private javax.swing.JTextField jtfMaPhieuXuat;
    private javax.swing.JTextField jtfSoLuong;
    // End of variables declaration//GEN-END:variables
}
